package edu.msse.signup.service;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;

import edu.msse.signup.domain.Event;
import edu.msse.signup.domain.Position;
import edu.msse.signup.domain.TimeSlot;
import edu.msse.signup.domain.Users;


import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class VolunteerRestWebService extends HttpServlet {
    WebApplicationContext spring;
    private static final long serialVersionUID = -2560989072138239258L;
    
    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        spring = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	EventService eventService = (EventService) spring.getBean("eventService");
    	TimeSlotService timeSlotService = (TimeSlotService) spring.getBean("timeSlotService");
    	PositionService positionService= (PositionService)spring.getBean("positionService");
    	/*
         * W-3: The system must provide a REST-style web service to retrieve positions for an event
         */
        
        if (request.getRequestURI().contains("event")) {
        	String id = request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/")+1);
            Event e=eventService.getEvent(Integer.parseInt(id));
            List<Position> positionList=eventService.getEventPositions(e);
            
            //create response
        	Document doc = DocumentFactory.getInstance().createDocument();
            Element root = doc.addElement("Event").addAttribute("name", e.getName());
            Iterator i=positionList.iterator();
            Position currPosition;
            
            while(i.hasNext()){
            	currPosition=(Position)i.next();
            	root.addElement("Position").addText(currPosition.getName());
            }
            response.getOutputStream().println(doc.asXML());
        }
        
        /*
         * W-4: The system must provide a REST-style web service to retrieve timeslots and signed up volunteers
         * for a position
         */
        if (request.getRequestURI().contains("position")) {
        	String id = request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/")+1);
            Position p=positionService.getPosition(Integer.parseInt(id));
            List<Users> userList=positionService.getUsers(p);
            List<TimeSlot> timeSlotList=positionService.getPositionTimeslots(p);
            //create response
        	Document doc = DocumentFactory.getInstance().createDocument();
            Element root = doc.addElement("Position").addAttribute("name", p.getName());
            Iterator i=userList.iterator();
            Users currUser;
            
            while(i.hasNext()){
            	currUser=(Users)i.next();
            	root.addElement("User").addText(currUser.getName());
            }
            Iterator j=timeSlotList.iterator();
            TimeSlot currTimeSlot;
            
            while(j.hasNext()){
            	currTimeSlot=(TimeSlot)j.next();
            	root.addElement("TimeSlot").addText(currTimeSlot.getTimeSlotBracket());
            }
            response.getOutputStream().println(doc.asXML());
        }
        
        /*
         * W-5: The system must provide a REST-style web service to retrieve volunteers signed up for a timeslot
         */
        if (request.getRequestURI().contains("timeSlot")) {
        	String id = request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/")+1);
            TimeSlot t=timeSlotService.getTimeSlot(Integer.parseInt(id));
            List<Users> userList=timeSlotService.getUsers(t);
            
            //create response
        	Document doc = DocumentFactory.getInstance().createDocument();
            Element root = doc.addElement("TimeSlot").addAttribute("Id", t.getTimeSlotId().toString());
            Iterator i=userList.iterator();
            Users currUser;
            
            while(i.hasNext()){
            	currUser=(Users)i.next();
            	root.addElement("User").addText(currUser.getName());
            }
            response.getOutputStream().println(doc.asXML());
        }
        
        
    }
}


